package com.openyelp.data.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.openyelp.data.core.Finder;
import com.openyelp.data.core.Pagination;
import com.openyelp.data.core.Updater;
import com.openyelp.data.dao.ShopDao;
import com.openyelp.data.dao.ShopReviewDao;
import com.openyelp.data.entity.Shop;
import com.openyelp.data.entity.ShopReview;
import com.openyelp.data.entity.UserInfo;
import com.openyelp.data.service.ShopReviewService;
import com.openyelp.data.service.UserService;

@Service
@Transactional
public class ShopReviewServiceImpl implements ShopReviewService {
	@Transactional(readOnly = true)
	public Pagination getPage(int pageNo, int pageSize) {
		Pagination page = dao.getPage(pageNo, pageSize);
		return page;
	}

	@Transactional(readOnly = true)
	public ShopReview findById(Long id) {
		ShopReview entity = dao.findById(id);
		return entity;
	}

	@Autowired
	ShopDao shopDao;

	@Autowired
	UserService userService;

	@Transactional
	public ShopReview save(ShopReview bean) {
		if (bean.getShop() == null || bean.getUser() == null) {

		} else {

			dao.save(bean);
			updatecomments(bean);

		}
		return bean;
	}

	private void updatecomments(ShopReview bean) {
		Shop shop = shopDao.findById(bean.getShop().getId());
		Finder finder = Finder
				.create("from ShopReview s where s.shop.id =:shopid");
		finder.setParam("shopid", shop.getId());
		int comments = dao.countQueryResult(finder);
		shop.setComment_num(comments);

		UserInfo user = userService.findById(bean.getUser().getId());
		Finder finderuser = Finder
				.create("from ShopReview s where s.user.id =:userid");
		finderuser.setParam("userid", user.getId());
        int usercomment=dao.countQueryResult(finderuser);
        user.setComments(usercomment);
		// userService

		bean.setAddDate(new Date());
		bean.setLastDate(new Date());
	}

	@Transactional
	public ShopReview update(ShopReview bean) {
		Updater<ShopReview> updater = new Updater<ShopReview>(bean);
		bean = dao.updateByUpdater(updater);
		return bean;
	}

	@Transactional
	public ShopReview deleteById(Long id) {

		ShopReview bean = dao.findById(id);
		updatecomments(bean);
		bean = dao.deleteById(id);

		return bean;
	}

	@Transactional
	public ShopReview[] deleteByIds(Long[] ids) {
		ShopReview[] beans = new ShopReview[ids.length];
		for (int i = 0, len = ids.length; i < len; i++) {
			beans[i] = deleteById(ids[i]);
		}
		return beans;
	}

	private ShopReviewDao dao;

	@Autowired
	public void setDao(ShopReviewDao dao) {
		this.dao = dao;
	}

	@Transactional(readOnly = true)
	@Override
	public Pagination pageByShop(Integer id, int pageNo, int pageSize) {
		Finder finder = Finder.create();
		finder.append("from ShopReview s where s.shop.id=:shopid order by id desc ");
		finder.setParam("shopid", id);
		return dao.find(finder, pageNo, pageSize);
	}

	@Transactional(readOnly = true)
	@Override
	public Pagination pageByUser(Long uid, int pageNo, int pageSize) {
		Finder finder = Finder.create();
		finder.append("from ShopReview s where s.user.id=:userid ");
		finder.setParam("userid", uid);
		finder.append(" order by s.id desc");
		return dao.find(finder, pageNo, pageSize);
	}
}